package com.xyx.ssm.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xyx.ssm.po.Syauthority;
import com.xyx.ssm.service.SyauthorityService;

@Controller
@RequestMapping(value = "/syauthority")
public class SyauthorityAction {
	@Resource(name = "syauthorityService")
	@Autowired
	private SyauthorityService syauthorityService;

	/**
	 * 查询所有数据
	 * 
	 * @return
	 */
	@RequestMapping("/syauthorityselect")
	@ResponseBody
	public List<Map<String, Object>> syauthorityadd() {
		return syauthorityService.selectSyauthorityadd();
	}

	/**
	 * 提供页面入口（权限管理页面）
	 */
	@RequestMapping("/syauthorityshouye")
	public String syauthority() {
		return "role/syauthority";
	}

	/**
	 * 增加权限
	 * 
	 * @throws IOException
	 */
	@RequestMapping("/syauthorityinsert")
	@ResponseBody
	public Map<String, Object> insert(Syauthority record) {
		if (record.getPid() != null && record.getPid() != "") {
			int num = syauthorityService.insert(record);
			Map<String, Object> mapjson = new HashMap<String, Object>();
			if (num > 0) {
				mapjson.put("flag", true);
				mapjson.put("msg", "添加成功！");
			} else {
				mapjson.put("flag", false);
				mapjson.put("msg", "添加失败！");
			}
			return mapjson;
		} else {
			Map<String, Object> mapjson = new HashMap<String, Object>();
			mapjson.put("flag", false);
			mapjson.put("msg", "列表不能为空！");
			return mapjson;
		}

	}

	/**
	 * 删除
	 */

	@RequestMapping("/syauthoritydelete")
	@ResponseBody
	public Map<String, Object> Syauthoritydelete(String id) {
		if(id!=null&id!=""){
		int num = syauthorityService.SyauthorityDelete(id);
		Map<String, Object> mapjson = new HashMap<String, Object>();
		if (num > 0) {
			mapjson.put("flag", true);
			mapjson.put("msg", "删除成功！");
		} else {
			mapjson.put("flag", false);
			mapjson.put("msg", "删除失败！");

		}
		return mapjson;
		}else{
			Map<String, Object> mapjson = new HashMap<String, Object>();
			mapjson.put("flag", false);
			mapjson.put("msg", "至少选择一个！");
			return mapjson;
		}
		
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/syauthorityupdate")
	@ResponseBody
	public Map<String, Object> Syauthorityupdate(Syauthority syauthority){
		int num=syauthorityService.SyauthorityUpdate(syauthority);
		Map<String, Object> mapjson = new HashMap<String, Object>();
		if(num>0){
			mapjson.put("flag", true);
			mapjson.put("msg", "修改成功！");
		}else{
			mapjson.put("flag", false);
			mapjson.put("msg", "修改失败！");
		}
		return mapjson ;
	}
	
}
